﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Web.Mvc;
using Project.Module;
using Project.Module.Common;
using Project.Module.DAO;
using Project.Module.DAO.BL;
using Project.Module.Model;
using Project.Engine.Control;
using Project.Engine.Utilities;

namespace Project.Module.View
{
    public class SearchView : BasePageRazor
    {
        #region
        public string displayuser = String.Empty;
        public string keyword = String.Empty;
        public string choice = "0";

        public List<ProductModel> ListResult { get; set; }

        public int totalRecord = 0;
        public int pageSize = DefineUtil.pageSizeSearch;
        public int pageIndex = 1;
        public int cateId = 0;

        private EventBL eventBl = new EventBL();

        #endregion

        protected override void InitializePage()
        {
            base.InitializePage();

            string url = Request.RawUrl.Replace("%20", " ");
            string cpage = UrlSearching.GetPageIdFromUrlSearch(url);
            pageIndex = Int32.TryParse(cpage, out pageIndex) ? pageIndex : 1;
            string ccateid = UrlSearching.GetCateIdFromUrl(url);
            cateId = Int32.TryParse(ccateid, out cateId) ? cateId : 0;

            if (url.IndexOf("tim-kiem") >= 0) {
                if (Request["keyword"] != null) {
                    keyword = Request["keyword"] != "  " ? UrlSearching.GetKeyFromKeyword(Request["keyword"]) : String.Empty;
                } else {
                    keyword = UrlSearching.GetKeyFromUrl(url);
                }
            }

            if (url.IndexOf("search") >= 0) {
                if (Request["choice"] == null) {
                    choice = Convert.ToString(cateId);
                } else {
                    choice = Request["choice"].ToString();
                }

                if (Request["key"] != null) {
                    keyword = Request["keyword"] != "  " ? UrlSearching.GetKeyFromKeyword(Request["key"]) : String.Empty;
                } else  {
                    keyword = UrlSearching.GetKeyFromUrl(url);
                }
            }

            keyword = SecurityUtil.RemoveSqlInjection(keyword);
            cateId = UrlSearching.GetCateId(choice);
            Init(keyword, pageIndex, cateId);
        }

        private void Init(string _keyword, int _pageIndex, int _cateId)
        {
            this.keyword = _keyword;
            this.pageIndex = _pageIndex;
            this.cateId = _cateId;
            if (Session["username"] != null && ConvertUtil.ToString(Session["username"]).Length > 0) {
                displayuser = ConvertUtil.ToString(Session["username"]);
            }

            if (String.IsNullOrEmpty(_keyword) == false)
            {
                ListResult = SearchProduct(this.cateId, this.keyword, this.pageIndex);
            }
        }

        private List<ProductModel> SearchProduct(int cateId, string keyword, int pageIndex)
        {
            List<ProductModel> list = (new SearchBL()).SearchProduct(cateId, keyword, (pageIndex * pageSize), pageSize, ref totalRecord);
            if (list != null)
            {
                foreach (var item in list)
                {
                    item.url = StringUtil.UnicodeToKoDauUrl(StringUtil.ShortContent(item.Description, 5));
                    item.ImagePath = eventBl.GetImagePathById(item.ImageID);
                    item.title = StringUtil.ShortContent(item.Description, 5);
                    item.summary = StringUtil.ShortContent(item.Description, 20);
                    item.CateName = ConvertUtil.GetCateNameUrlByCateId(item.CatagoryId);
                }
            }

            return list;
        }
    }
}

